/**
* Created by wust on 2020/4/9.
*/
<template>
  <div id="work-order-distribute">
    <el-form ref="formModel" :model="formModel" :rules="rules" label-width="120px" style="width:70%;" @submit.native.prevent>
      <el-form-item label="负责人" prop="director">
        <el-cascader v-model="directorCascaderValue" :options="directorCascaderOptions" @change="directorCascaderHandleChange"></el-cascader>
      </el-form-item>
      <el-form-item style="text-align: left;"><el-button type="primary" @click="onSubmit('formModel')" :loading="submitting">提交</el-button></el-form-item>
    </el-form>
  </div>
</template>
<script>
import Vue from 'vue'

export default {
  name: 'work-order-distribute',
  props: ['selectedModel'],
  data () {
    return {
      submitting: false,
      formModel: {
        workOrderTypeId: null,
        director: null
      },
      rules: {
        director: [{required: true, message: '请选择负责人', trigger: 'blur'}]
      },
      directorCascaderOptions: [],
      directorCascaderValue: []
    }
  },
  created: function () {
    this.formModel = this.selectedModel
    this.initDirectorCascaderOption()
  },
  methods: {
    directorCascaderHandleChange: function (val) {
       this.formModel.director = val[val.length - 1]
    },
    initDirectorCascaderOption: function () {
      Vue.$ajax({
        method: 'post',
        url: Vue.$workOrderServerURL + '/web/v1/WorkOrderUserController/buildCascaderByWorkType?workType=' + this.formModel.workOrderTypeId
      }).then(res => {
        if (res.data.flag !== 'SUCCESS') {
          if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        } else {
          this.directorCascaderOptions = res.data.obj
        }
      })
    },
    onSubmit: function(formData) {
      this.$refs[formData].validate(valid => {
        if (!valid) {
        } else {
          this.submitting = true;
          Vue.$ajax({
            method: 'post',
            url: Vue.$workOrderServerURL + '/web/v1/WorkOrderController/distribute?workOrderId=' + this.formModel.id + '&director=' + this.formModel.director
          }).then(res => {
            this.submitting = false;
            if (res.data.flag !== 'SUCCESS') {
              if (!Vue.$isNullOrIsBlankOrIsUndefined(res.data.message)) {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                });
              }
            } else {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
               this.$emit('closeDialog', null)
            }
          })
        }
      });
    }
  }
}
</script>
